<template>
  <card>
    <p slot="title">
        <Icon type="compose"></Icon>
        基本信息
    </p>
    <Form ref="formItem" :model="formItem" :rules="ruleForm" :label-width="120">
        <Row type="flex">
            <Col span="6">
                <FormItem label="项目名称：" prop="name">
                    <Input v-model="formItem.name" size="large" placeholder="请填写内容...">
                    </Input>
                </FormItem>
            </Col>
            <Col span="6">
                <FormItem label="项目价格：" prop="price">
                    <InputNumber style="width: 100%" :min="0" v-model="formItem.price" placeholder="请输入价格..."></InputNumber>
                </FormItem>
            </Col>
        </Row>
        <Row type="flex">
            <Col span="24">
                <FormItem>
                    <Button @click="save" size="large" type="primary">保存</Button>
                    <Button @click="close" size="large" type="ghost" style="margin-left: 8px">关闭</Button>
                </FormItem>
            </Col>
        </Row>
    </Form>
  </card>
</template>

<script>
export default {
    data () {
        return {
            baseURL: '/user/service/project',
            brands: [],
            formItem: {
                id: '',
                name: '',
                price: ''
            },
            ruleForm: {
                name: [
                    { required: true, message: '请填写项目名称', pattern: /.+/, trigger: 'change' }
                ],
                price: [
                    { required: true, message: '请输入项目价格', pattern: /.+/, trigger: 'change' }
                ]
            }
        }
    },
    methods: {
        init () {
            // 表单初始化
            this.reset()
            this.$bus.fillData(this.formItem, this.$route.query.row)
        },
        save () {
            this.$refs['formItem'].validate((valid) => {
                if (valid) {
                    let url = this.baseURL + '/saveOrUpdate'
                    this.$ajax.post(url, this.formItem).then((response) => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', msg)
                            this.$bus.init(this.formItem)
                            if (this.formItem.id === '') {
                                this.reset()
                            }
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            })
        },
        close () {
            this.$store.state.app.close = true
            this.$bus.closePageToLink()
        },
        reset () {
            this.$refs.formItem.resetFields()
        }
    },
    beforeRouteEnter (to, from, next) {
        next(vm => {
            vm.init()
        })
    },
    beforeRouteLeave (to, from , next) {
        this.$bus.routeLeave(next, this.selectTabPane)
    }
}
</script>

<style>

</style>
